package sessionBeans;

import java.io.Serializable;
import java.util.UUID;
import java.util.Collection;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;


import objects.AbilityObject;
import objects.CommentObject;
import objects.HelpmeetingObject;
import objects.MessageObject;
import objects.RequestNotificationObject;
import objects.UserObject;

import org.jboss.ejb3.annotation.RemoteBinding;

import entityBeans.Ability;
import entityBeans.Comment;
import entityBeans.Helpmeeting;
import entityBeans.Message;
import entityBeans.RequestNotification;
import entityBeans.User;

@Stateless
@RemoteBinding( jndiBinding = "AccessRemoteJNDI" )
public class AccessBean implements AccessRemote{
	
	private EntityManager manager;
	
	@EJB private UserFacadeLocal userFacadeLocal;
	@EJB private SearchFacadeLocal searchFacadeLocal;
	@EJB private AbilityFacadeLocal abilityFacadeLocal;
	@EJB private HelpmeetingFacadeLocal helpmeetingFacadeLocal;
	
	private User user;
	
	private static final String AUTHORIZATION_MESSAGE = "Operation not permitted!";
	private static final String USER_ERROR ="User not foud";

	@PostConstruct
	void postConstruct() {
		user = null;	
	}
		
	@Override
	public Serializable login(String email, String password)
			throws BeanException {
		
		Serializable ret=null;
//		int count=0;
		
		user = manager.find(User.class, email);
		if(user!=null){
			
			if(user.getPassword().equals(password)){
				ret=new UserObject(user);
				return ret;
			}else{
				throw new BeanException("Wrong password!!");
			}
			
		}
			
			throw new BeanException("You are not registered! You can register the website by clicking register button!");		
	}

	@Override
	public void logout() throws BeanException {
		
		if(user!=null){
			user=null;
		}else{
			throw new BeanException("There is no active login!");
		}		
	}

	@Override
	public Collection<HelpmeetingObject> getHelpmeetings(User active) throws BeanException {
		user=active;
		
		if(user!=null){
			return userFacadeLocal.getHelpmeetingsOfUser(user);
		}else{
			throw new BeanException(USER_ERROR);
		}
	}
	
	@Override
	public Collection<MessageObject> getMessages(User active) throws BeanException {
		user=active;

		if(user!=null){
			return userFacadeLocal.getMessagesOfUser(user);
		}else{
			throw new BeanException(USER_ERROR);
		}
	}
	
	@Override
	public Collection<AbilityObject> getAbilities(User active) throws BeanException {
		user=active;

		if(user!=null){
			return userFacadeLocal.getAbilitiesOfUser(user) ;
		}else{
			throw new BeanException(USER_ERROR);
		}
	}
	
	@Override
	public Collection<RequestNotificationObject> getNotifications(User active) throws BeanException {
		user=active;

		if(user!=null){
			return userFacadeLocal.getNotificationsOfUser(user) ;
		}else{
			throw new BeanException(USER_ERROR);
		}
	}

	
	
	//
	@Override
	public void addAbility(User Active, Ability ability) throws BeanException {
		user=Active;
		if(user==null){
			throw new BeanException(USER_ERROR);
		}else{
			userFacadeLocal.addAbility(user, ability);
		}
		
	}
	
	

	@Override
	public void sendMessage(User Active, Message message, RequestNotification request) throws BeanException {
		user=Active;
		if(user != null){
			UUID id = UUID.randomUUID();
			String sid=id.toString();
			message.setMId(sid);
			
			id= UUID.randomUUID();
			sid= id.toString();
			request.setNId(sid);
			userFacadeLocal.addMessage(user, message, request);
		}else{
			throw new BeanException(USER_ERROR);
		}
	}

	@Override
	public void commentOnMeeting(User Active, HelpmeetingObject meeting,
			Comment comment) throws BeanException {
				
		user=Active;
		if(user != null){
			if(meeting!=null){
				if(comment.getText()!=null){
					
					UUID id = UUID.randomUUID();
					String sid=id.toString();
					comment.setCId(sid);
					
					helpmeetingFacadeLocal.commentOnMeeting(user, meeting, comment);
					
				}else{
					throw new BeanException("Comment can not be empty!");
				}
				
			}else{
				throw new BeanException("Empty meeeting there is a problem!");
			}
			
		}else{
			throw new BeanException(USER_ERROR);
		}
		
	}

	@Override
	public void addHelpmeeting(User Active, Helpmeeting meeting) throws BeanException {
		user=Active;
		if(user != null){
			UUID id = UUID.randomUUID();
			String sid=id.toString();
			meeting.setMeetingID(sid);
			
			userFacadeLocal.addHelpmeeting(user, meeting);
			
		}else{
			throw new BeanException(USER_ERROR);
		}
	}

	//think about this again when you concentrated


	/*
	 * Tested it seems ok
	 *
	 * (non-Javadoc)
	 * @see sessionBeans.AccessRemote#searchHelp(java.lang.String)
	 */
	@Override
	public Collection<AbilityObject> searchHelp(String keyword)
			throws BeanException {
		
			return searchFacadeLocal.searchAbility(keyword);		
		
	}

	/*
	 * Tested it seems ok
	 * 
	 * (non-Javadoc)
	 * @see sessionBeans.AccessRemote#searchFriend(java.lang.String)
	 */
	@Override
	public Collection<UserObject> searchFriend(String keyword)
			throws BeanException {
		
		if(user != null){
			return searchFacadeLocal.searchFriend(keyword);		
		}else{
			throw new BeanException(USER_ERROR);
		}
	}

	
	/*
	 * 
	 * Different User_ID creator?
	 * 
	 * (non-Javadoc)
	 * @see sessionBeans.AccessRemote#register(objects.UserObject)
	 */
//	@Override
//	public Serializable register(UserObject userObject) throws BeanException {
//		
//		if(userObject.getEmail()==null || userObject.getPassword()==null){
//			throw new BeanException("Email and password parts can not be empty!");
//		}else{
//			// we can also write constructor for this part to user
//			User userToSave= new User();
//			userToSave.setBday(userObject.getbday());
//			userToSave.setCity(userObject.getCity());
//			userToSave.setEmail(userObject.getEmail());
//			userToSave.setGender(userObject.getGender());
//			userToSave.setJob(userObject.getJob());
//			userToSave.setName(userObject.getName());
//			userToSave.setPassword(userObject.getPassword());
//			userToSave.setSurname(userObject.getSurname());
//			
//			UUID id = UUID.randomUUID();
//			String userid=id.toString();
//			
//			userToSave.setUserID(userid);
//			
//			manager.persist(userToSave);
//			
//			return userToSave;
//			
//		}
//		
//	}

	@Override
	public void requestAbility(User Active, AbilityObject ability, RequestNotificationObject request) throws BeanException {
		user=Active;
		
		if(user!=null){
			
			UUID id = UUID.randomUUID();
			String abilityid=id.toString();
			ability.setaId(abilityid);
			
			id = UUID.randomUUID();
			String requestid=id.toString();
			request.setnId(requestid);
			
			abilityFacadeLocal.requestAbility(ability,request);
		}else{
			throw new BeanException(USER_ERROR);
		}
	}

	@Override
	public String register(String name, String surname, String email,
			String city, String job, String bday, String password, String gender)
			throws BeanException {
		
		UserObject user= new UserObject();
		user.setbday(bday);
		user.setCity(city);
		user.setEmail(email);
		user.setgender(gender);
		user.setJob(job);
		user.setName(name);
		user.setPassword(password);
		user.setSurname(surname);
		
		
		if(user.getEmail()==null || user.getPassword()==null){
			throw new BeanException("Email and password parts can not be empty!");
		}else{
			// we can also write constructor for this part to user
			User userToSave= new User();
			userToSave.setBday(user.getbday());
			userToSave.setCity(user.getCity());
			userToSave.setEmail(user.getEmail());
			userToSave.setGender(user.getGender());
			userToSave.setJob(user.getJob());
			userToSave.setName(user.getName());
			userToSave.setPassword(user.getPassword());
			userToSave.setSurname(user.getSurname());
			
			UUID id = UUID.randomUUID();
			String userid=id.toString();
			
			
			userToSave.setUserID(userid);
			
			System.out.println(userid);
			System.out.println(userToSave.getEmail());
			
			manager.persist(userToSave);
			
			return userid;
			
		}
		
	
	}

	@Override
	public void acceptRejectRequest(User Active, RequestNotification request, boolean flag)
			throws BeanException {
		user=Active;
		
		if(user!=null){
			if(!request.getCheck()){
				if(request.getType()=="Friendship"){
					if(!flag){
						request.setAccepted(flag);
						request.setCheck(true);
						manager.persist(request);
						
					}else{
						request.setCheck(true);
						userFacadeLocal.addFriend(user, request);
					}
				}else if(request.getType()=="Ability"){
					if(!flag){
						request.setAccepted(flag);
						request.setCheck(true);
						manager.persist(request);
					}else{
						abilityFacadeLocal.acceptAbility(request);
					}
				}else if (request.getType()=="Message"){
					request.setCheck(true);
					manager.persist(request);
					
				
				}else{
					throw new BeanException("There is no such a type of request or notification!");
				}
				
			}else{
				throw new BeanException("This request is already shown!");
			}
		}else{
			throw new BeanException(USER_ERROR);
		}
	}

	@Override
	public Collection<Ability> getAllAbilities() throws BeanException {
		
		return abilityFacadeLocal.takeListOfAbility();
	}

	@Override
	public Collection<UserObject> getFriends(User active) throws BeanException {
		user=active;
		
		if(user!=null){
			return userFacadeLocal.getFriendsOfUser(active);
		}else{
			throw new BeanException(USER_ERROR);
		}
		
	}

	@Override
	public void createAbility() throws BeanException {
		
	}

}